package com.leetcode.LC;

public class LC21 {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode listNode = new ListNode();
        if (l1==null&&l2==null){
            return null;
        }
        if (l1==null&&l2!=null){
            return l2;
        }
        if (l2==null&&l1!=null){
            return l1;
        }
        ListNode a = l1, b = l2, c = listNode;

        while (a != null && b != null) {
            if (a.val <= b.val) {
                c.val = a.val;
                if (a.next != null) {
                    c.next = new ListNode();
                    c = c.next;
                }
                a = a.next;
            } else {
                c.val = b.val;
                if (b.next != null) {
                    c.next = new ListNode();
                    c = c.next;
                }
                b = b.next;
            }

        }
        if (a != null) {
            c.next = a;
        }
        if (b != null) {
            c.next = b;
        }
        return listNode;
    }

    public static void main(String[] args) {

    }
}
/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode() {}
 * ListNode(int val) { this.val = val; }
 * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */